package com.huxin.servelt;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.BookHisInfo;
import com.huxin.pojo.ConditionalQueryDTO;
import com.huxin.pojo.PageBean;
import com.huxin.pojo.UserInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectreader")
public class SLAreaderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求方法
        request.setCharacterEncoding("utf-8");
        //处理响应乱码
        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();
        ConditionalQueryDTO conditionalQueryDTO = JSON.parseObject(s, ConditionalQueryDTO.class);
        Userservice service = new Userservice();
        int page=1;//开始页
        int size=10;//每页显示十条数据

        PageBean<UserInfo> userInfoPageBean = null;
        //conditionalQueryDTO.getPage().equals("null")||conditionalQueryDTO.getPage().equals(" ")||conditionalQueryDTO.getPage()==null||conditionalQueryDTO.getPage()==0
        if (conditionalQueryDTO==null){//按照登录账号查询用户
            userInfoPageBean=service.selectallreader(page,size);
        } else if ("name".equals(conditionalQueryDTO.getType())) {//按照用户名查询用户
            userInfoPageBean=service.selectUserByname(conditionalQueryDTO.getText(),conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        }else if ("username".equals(conditionalQueryDTO.getType())){
            userInfoPageBean =service.selectUserByUsername(conditionalQueryDTO.getText(),conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        }else {//查询所有
            userInfoPageBean=service.selectallreader(conditionalQueryDTO.getPage(),conditionalQueryDTO.getSize());
        }



        String str= JSON.toJSONString(userInfoPageBean);
        System.out.println(str);
        response.getWriter().write(str);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
